package com.yunbao.web.interceptor;

import com.yunbao.web.servlet.HandlerMethod;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by louisyuu on 2020/3/12 下午5:45
 */
public class HandlerInterceptorRegistry {


    /**
     * handler method余拦截器的映射关系
     * 因为一个controller的方法是可以配置多个拦截器的
     * 例如下面的配置
     * <mvc:interceptors>
     * <mvc:interceptor>
     * <mvc:mapping path="/order/list"/>
     * <bean class="com.lagou.edu.interceptor.MyIntercepter01"/>
     * </mvc:interceptor>
     * <p>
     * <mvc:interceptor>
     * <mvc:mapping path="/order/list"/>
     * <bean class="com.lagou.edu.interceptor.MyIntercepter02"/>
     * </mvc:interceptor>
     * </mvc:interceptors>
     * 这个/order/list URI所对象的
     * <p>
     * 但是我们没有完成这复杂的映射关系，所以这里暂定为一个handler method对应所有的HandlerInterceptors
     */
    private Map<HandlerMethod, Collection<HandlerInterceptor>> handlerMethodInterceptorMapping = new HashMap<>();


    private HandlerInterceptorRegistry() {
    }

    private final static HandlerInterceptorRegistry instance = new HandlerInterceptorRegistry();

    public static HandlerInterceptorRegistry getInstance() {
        return instance;
    }


    public Map<HandlerMethod, Collection<HandlerInterceptor>> getHandlerMethodInterceptorMapping() {
        return handlerMethodInterceptorMapping;
    }


    public void setHandlerMethodInterceptorMapping(Map<HandlerMethod, Collection<HandlerInterceptor>> handlerMethodInterceptorMapping) {
        this.handlerMethodInterceptorMapping = handlerMethodInterceptorMapping;
    }

    public Collection<HandlerInterceptor> getHandlerInterceptors(HandlerMethod handlerMethod) {
        return this.getHandlerMethodInterceptorMapping().get(handlerMethod);
    }


    public void registerHandlerInterceptors(HandlerMethod handlerMethod, Collection<HandlerInterceptor> handlerInterceptors) {
        this.getHandlerMethodInterceptorMapping().put(handlerMethod, handlerInterceptors);
    }


}
